let path = getDataPath();
// 用户ID
let id = getUrlParam("id");
$(function () {
    GetUserRole();
    // loadFnc();
});
//获取用户权限
function GetUserRole() {
    // let thisdata = [];
    // thisdata.push(InitRole(id));
    let USERdata = InitRole(id);
    if (USERdata != null) {
        loadFnc();
    }
    else OpenErrmsg("web/errhtml/404.html");
}
function loadFnc(params) {
    ReInitData()
}

function ReInitData() {
    var endtime = GetDateStr(0)
    var starttime = GetDateStr(-1)
    $('#db_Start').datebox({
        onSelect: function (date) {
            var y = date.getFullYear();//年
            var m = date.getMonth() + 1;//月
            var d = date.getDate();//日
            var ymd = y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
            starttime = ymd
            if (starttime >= endtime) {
                alert('开始时间不能晚于结束时间')
                reset()
            } else {
                InitChart(endtime, starttime)
            }
        }
    });
    $('#db_End').datebox({
        onSelect: function (date) {
            var y = date.getFullYear();//年
            var m = date.getMonth() + 1;//月
            var d = date.getDate();//日
            var ymd = y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
            endtime = ymd
            if (starttime >= endtime) {
                alert('开始时间不能晚于结束时间')
                reset()
            } else {
                InitChart(endtime, starttime)
            }
        }
    });
    $('#db_Start').datebox('setValue', GetDateStr(-1));
    $('#db_End').datebox('setValue', getCurrDate());

    InitChart(endtime, starttime)
}
function reset() {
    var endtime = GetDateStr(0)
    var starttime = GetDateStr(-1)
    setTimeout(() => {
        $('#db_Start').datebox('setValue', GetDateStr(-1));
        $('#db_End').datebox('setValue', getCurrDate());
    }, 1000);
    InitChart(endtime, starttime)
}
//折线图
function InitChart(endtime, starttime) {
    let xAxis = [];
    let data1 = [];
    let data2 = [];
    let data3 = [];
    let data4 = [];
    let data5 = [];
    let data6 = [];
    let data7 = [];
    let data8 = [];
    let data9 = [];
    let data10 = [];
    let data11 = [];
    let data12 = [];
    let data13 = [];
    let data14 = [];
    let data15 = [];
    let data16 = [];
    let data17 = [];
    let data18 = [];
    let data19 = [];
    let data20 = [];
    let data21 = [];
    let data22 = [];
    let data23 = [];
    let data24 = [];
    let data25 = [];
    let data26 = [];
    let data27 = [];
    let data28 = [];
    let data29 = [];
    let data30 = [];
    let data31 = [];
    let data32 = [];
    let data33 = [];
    let data34 = [];
    let url = ''
    url = path + '/ls172/data-analysis/makeReport'
    var params = {
        "endTime": endtime,
        "startTime": starttime
    }
    $.ajax({
        url: url,
        type: "post",
        async: false,
        dataType: "json",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json"
        },
        data: JSON.stringify(params),

        success: function (data) {
            xAxis = data.data.replaceAllList
            data1 = data.data.one[0].vals
            data2 = data.data.one[2].vals
            data3 = data.data.one[1].vals
            data4 = data.data.one[7].vals
            data5 = data.data.one[5].vals
            data6 = data.data.one[10].vals
            data7 = data.data.one[8].vals
            data8 = data.data.one[12].vals
            data9 = data.data.two[0].vals
            data10 = data.data.two[2].vals
            data11 = data.data.two[1].vals
            data12 = data.data.two[7].vals
            data13 = data.data.two[5].vals
            data14 = data.data.two[10].vals
            data15 = data.data.two[8].vals
            data16 = data.data.two[12].vals
            data17 = data.data.threeOne[0].vals
            data18 = data.data.threeOne[2].vals
            data19 = data.data.threeOne[1].vals
            data20 = data.data.threeOne[7].vals
            data21 = data.data.threeOne[5].vals
            data22 = data.data.threeOne[10].vals
            data23 = data.data.threeOne[8].vals
            data24 = data.data.threeTwo[0].vals
            data25 = data.data.threeTwo[2].vals
            data26 = data.data.threeTwo[1].vals
            data27 = data.data.threeTwo[7].vals
            data28 = data.data.threeTwo[5].vals
            data29 = data.data.threeTwo[10].vals
            data30 = data.data.threeTwo[8].vals
            data31 = data.data.threeTwo[8].vals
            data32 = data.data.threeTwo[8].vals
            data33 = data.data.threeTwo[8].vals
            data34 = data.data.threeTwo[8].vals
            createChart("chartdiv1", xAxis, data1, "一级压缩机入口温度 ");
            createChart("chartdiv2", xAxis, data2, "一级压缩机A出口压力");
            createChart("chartdiv3", xAxis, data3, "一级压缩机A出口温度");
            createChart("chartdiv4", xAxis, data4, "一级压缩机B出口压力");
            createChart("chartdiv5", xAxis, data5, "一级压缩机B出口温度");
            createChart("chartdiv6", xAxis, data6, "一级洗涤器压力");
            createChart("chartdiv7", xAxis, data7, "一级洗涤器液位");
            createChart("chartdiv8", xAxis, data8, "一级后冷却器出口温度");
            createChart("chartdiv9", xAxis, data9, "二级压缩机入口温度");
            createChart("chartdiv10", xAxis, data10, "二级压缩机A出口压力");
            createChart("chartdiv11", xAxis, data11, "二级压缩机A出口温度");
            createChart("chartdiv12", xAxis, data12, "二级压缩机B出口压力");
            createChart("chartdiv13", xAxis, data13, "二级压缩机B出口温度");
            createChart("chartdiv14", xAxis, data14, "二级洗涤器压力");
            createChart("chartdiv15", xAxis, data15, "二级洗涤器液位");
            createChart("chartdiv16", xAxis, data16, "二级后冷却器出口温度");
            createChart("chartdiv17", xAxis, data17, "三级第一压缩机入口温度");
            createChart("chartdiv18", xAxis, data18, "三级第一压缩机A出口压力");
            createChart("chartdiv19", xAxis, data19, "三级第一压缩机A出口温度");
            createChart("chartdiv20", xAxis, data20, "三级第一压缩机B出口压力");
            createChart("chartdiv21", xAxis, data21, "三级第一压缩机B出口温度");
            createChart("chartdiv22", xAxis, data22, "三级第一洗涤器压力");
            createChart("chartdiv23", xAxis, data23, "三级第一洗涤器液位");
            createChart("chartdiv24", xAxis, data24, "三级第二压缩机入口温度");
            createChart("chartdiv25", xAxis, data25, "三级第二压缩机A出口压力");
            createChart("chartdiv26", xAxis, data26, "三级第二压缩机A出口温度");
            createChart("chartdiv27", xAxis, data27, "三级第二压缩机B出口压力");
            createChart("chartdiv28", xAxis, data28, "三级第二压缩机B出口温度");
            createChart("chartdiv29", xAxis, data29, "三级第二洗涤器压力");
            createChart("chartdiv30", xAxis, data30, "三级第二洗涤器液位");
            createChart("chartdiv31", xAxis, data30, "温度数据");
            createChart("chartdiv32", xAxis, data30, "压力数据");
            createChart("chartdiv33", xAxis, data30, "温度数据");
            createChart("chartdiv34", xAxis, data30, "压力数据");
            let yuzhidata = data.data.siren
            let guzhangdata = data.data.other
            $('#dg_DateList1').datagrid({
                data: {
                    "total": yuzhidata.length, "rows": yuzhidata
                }
            });
            $('#dg_DateList2').datagrid({
                data: {
                    "total": guzhangdata.length, "rows": guzhangdata
                }
            });
        },
        error: function (data) {
            console.log("code", data);
        }
    });
}

function createChart(id, xAxis, data, title) {
    let myChart = echarts.init(document.getElementById(id));
    let xAxisDataLine = new Array();
    for (let i = 0; i < data.length; i++) {
        if (data[i] == '-9999') {
            data[i] = '0'
        }
    }
    // console.log(xAxisDataLine)
    // 指定图表的配置项和数据
    let option = {
        title: {
            text: title,
            padding: 15,
            textStyle: {
                fontSize: 12,
                color: '#333'          // 主标题文字颜色
            },
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: xAxisDataLine,

        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            // feature: {
            //     saveAsImage: {}
            // }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xAxis
        },
        yAxis: {
            type: 'value'
        },
        series: {
            data: data,
            type: 'line'
        }
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}